package top.coolrainly.module.resource.service.content;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import top.coolrainly.module.resource.controller.admin.content.vo.*;
import top.coolrainly.module.resource.dal.dataobject.content.ResourceContentDO;
import top.coolrainly.framework.common.pojo.PageResult;
import top.coolrainly.framework.common.pojo.PageParam;
import top.coolrainly.framework.common.util.object.BeanUtils;

import top.coolrainly.module.resource.dal.mysql.content.ResourceContentMapper;

import static top.coolrainly.framework.common.exception.util.ServiceExceptionUtil.exception;
import static top.coolrainly.module.resource.enums.ErrorCodeConstants.*;

/**
 * Web3D 资源 内容 Service 实现类
 *
 * @author CoolRainLy
 */
@Service
@Validated
public class ResourceContentServiceImpl implements ResourceContentService {

    @Resource
    private ResourceContentMapper contentMapper;

    @Override
    public Long createContent(ResourceContentSaveReqVO createReqVO) {
        // 插入
        ResourceContentDO content = BeanUtils.toBean(createReqVO, ResourceContentDO.class);
        contentMapper.insert(content);
        // 返回
        return content.getId();
    }

    @Override
    public void updateContent(ResourceContentSaveReqVO updateReqVO) {
        // 校验存在
        validateContentExists(updateReqVO.getId());
        // 更新
        ResourceContentDO updateObj = BeanUtils.toBean(updateReqVO, ResourceContentDO.class);
        contentMapper.updateById(updateObj);
    }

    @Override
    public void deleteContent(Long id) {
        // 校验存在
        validateContentExists(id);
        // 删除
        contentMapper.deleteById(id);
    }

    private void validateContentExists(Long id) {
        if (contentMapper.selectById(id) == null) {
            throw exception(CONTENT_NOT_EXISTS);
        }
    }

    @Override
    public ResourceContentDO getContent(Long id) {
        return contentMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceContentDO> getContentPage(ResourceContentPageReqVO pageReqVO) {
        return contentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResourceContentDO> getContentList(Integer type) {
        return contentMapper.selectListByType(type);
    }

    @Override
    public List<ResourceContentDO> getContentListByGroupId(Long groupId) {
        return contentMapper.selectList(ResourceContentDO::getGroupId, groupId);
    }

}